<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <el-input class="w-50" v-model="form.order_no" placeholder="相关单号" />
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 150px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()"
            style="max-width: 300px" />
        </VGroup>
      </template>
      <template #form_handle>
        <el-button type="primary" @click="queryForm()">查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </template>
      <template #toolbar_btns></template>
    </VTable>
    <div class="fixed left-0 right-0 bottom-4 text-center">
      <el-button type="" @click="handleBack">返回</el-button>
    </div>
  </VPage>
</template>
<script setup name="StatementInventory-balanceWare" lang="jsx">
import { ElMessage, ElMessageBox } from 'element-plus'
import { nextTick, ref, reactive, toRefs, onMounted } from 'vue'
import { getWareLog } from '../api/index'
import action from '@/utils/action.js'
import { useRoute } from 'vue-router';
const route = useRoute()

const timeOption = [
  { label: "更新时间", value: "update_time" },
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 80 },
  formConfig: {
    data: {
      timeRange: { type: 'update_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'seq', title: '序号', width: "80" },
    {
      field: 'op_type_cn', title: '操作类型', width: 120
    },
    {
      field: 'order_no', title: '相关单号', width: 200
    },
    {
      field: 'warehouse_name', title: '仓库', width: 120
    },
    {
      field: 'after_occupy_num', title: '当前占用', width: 120
    },
    {
      field: 'op_occupy_num', title: '占用变化', width: 140
    },
    {
      field: 'after_transit_num', title: '当前在途', width: 160
    },
    {
      field: 'op_transit_num', title: '在途变化', width: 160
    },
    {
      field: 'op_now_num', title: '当前库存', width: 100
    },
    {
      field: 'after_now_num', title: '当前库存变化', width: 120
    },
    {
      field: 'after_num', title: '当前可用', width: 100
    },
    {
      field: 'op_num', title: '可用变化', width: 100
    },
    {
      field: 'after_lock_num', title: '当前锁库', width: 120
    },
    {
      field: 'op_lock_num', title: '锁库变化', width: 120
    },
    {
      field: 'after_ready_shipment_num', title: '当前待发', width: 120
    },
    {
      field: 'op_ready_shipment_num', title: '待发变化', width: 120
    },
    {
      field: 'create_user', title: '操作人', width: 160
    },
    {
      field: 'create_time', title: '更新时间', width: 180
    },
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await getWareLog({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})

const data = reactive({
  form: {},
})
const { form } = toRefs(data)

onMounted(() => {
  if (route?.query) {
    form.value = {
      sku: route?.query.sku,
      warehouse_id: route?.query.warehouse_id,
    }
    tableRef.value.setForm({ ...form.value })
  }
})

const queryForm = () => {
  nextTick(() => {
    tableRef.value.query()
  })
}

const resetForm = () => {
  nextTick(() => {
    tableRef.value.setForm({ ...form.value })
    tableRef.value.query()
  })
}

const handleBack = () => {
  action.setGlobalState({
    changeMicoTabsPath: {
      type: 'closeActiveTab',
    }
  })
}

</script>